<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogVisible"
    :lock-scroll="true"
    :close-on-click-modal="true"
    :close-on-press-escape="true"
    width="640px"
    :append-to-body="true"
    class="h-dialog"
    :modal-append-to-body="true" style="font-size: 0.8rem;"
  >
    <div class="container-fluid">
      <div class="row">
        <el-form ref="form" :rules="rules" :model="task" label-width="110px">
          <el-form-item label="任务名称:" prop="name">
            <el-input v-model="task.name"></el-input>
            <span class="field-info">请填写任务名称</span>
          </el-form-item>

          <el-form-item label="任务代码:" prop="jobName">
            <el-input v-model="task.jobName"></el-input>
            <span class="field-info">请填写任务代码</span>
          </el-form-item>

          <el-form-item label="任务组名:" prop="jobGroup">
            <el-input v-model="task.jobGroup"></el-input>
            <span class="field-info">请填写任务组的名称</span>
          </el-form-item>
          <el-form-item label="任务调用类:" prop="jobTrigger">
            <el-input v-model="task.jobTrigger"></el-input>
            <span class="field-info">请填写要调用的任务类的全名称</span>
          </el-form-item>
          <el-form-item label="任务调度时间:" prop="cronExpression">
            <el-input v-model="task.cronExpression"></el-input>
            <span class="field-info">请填写任务的调用的cron表达式</span>
          </el-form-item>
          <el-form-item label="任务描述:">
            <el-input type="textarea" :row="2" v-model="task.description"></el-input>
            <span class="field-info">请填写任务的描述信息</span>
          </el-form-item>
          <el-form-item label="任务参数:">
            <el-input type="textarea" :row="2" v-model="task.params"></el-input>
            <span class="field-info">请填写任务的参数信息</span>
          </el-form-item>

        </el-form>
      </div>
    </div>
    <div slot="footer">
      <el-button type="primary" @click="submitTask" :loading="loading"><i class="fa  fa-plus"/> 添加任务</el-button>
      <el-button type="danger" @click="closed"><i class="fa  fa-sign-out"/> 关闭</el-button>
    </div>
  </el-dialog>

</template>

<script>
  import Dialog from 'src/components/common/js/dialog-mix'
  import SysService from 'src/service/sys'

  export default {
    name: 'HTaskEditDialog',
    mixins: [Dialog],
    data() {
      return {
        task: {},
        title: '添加任务',
        loading: false,
        rules: {
          name: [
            {required: true, message: '请填写任务名称', trigger: 'blur'},
            {min: 1, message: '请填写任务名称', trigger: 'blur'}
          ],
          jobName: [
            {required: true, message: '请填写任务代码', trigger: 'blur'},
            {min: 1, message: '请填写任务代码', trigger: 'blur'}
          ],
          jobGroup: [
            {required: true, message: '请填写任务组', trigger: 'blur'},
            {min: 1, message: '请填写任务组', trigger: 'blur'}
          ],
          jobTrigger: [
            {required: true, message: '请填写任务执行类', trigger: 'blur'},
            {min: 1, message: '请填写任务执行类', trigger: 'blur'}
          ],
          cronExpression: [
            {required: true, message: '请填写cron表达式', trigger: 'blur'},
            {min: 1, message: '请填写cron表达式', trigger: 'blur'}
          ]
        }
      }
    },
    watch: {
      value: function (c, d) {
        if (c != null) {
          this.title = "修改任务";
          this.task = c;
        } else {
          this.title = "添加任务";
          this.initData();
        }
      }
    },
    props: {
      value: {
        type: Object,
      }
    },
    methods: {
      //提交任务
      submitTask: function () {
        const me = this;
        this.$refs['form'].validate((valid) => {
          if (valid) {
            SysService.saveTask(me.task).then(d => {
              if (d != null) {
                me.$message.success('添加新任务成功。')
                me.$emit('success', d.datas[0])
              }
            })
          } else {
            return false
          }
        })
      },
      initData: function () {
        this.task = {
          id: null,
          name: null,
          jobName: null,
          jobGroup: null,
          jobTrigger: null,
          cronExpression: null,
          description: null,
          params: null
        }
      }
    },
    beforeMount: function () {
      if (this.value == null) {
        this.title = "添加任务";
        this.initData();
      } else {
        this.title = "修改任务";
        this.task = this.value;
      }
    }
  }
</script>
